Content starts here Create Conditional Elements in Return Types
This page last changed on Feb 26, 2008.

eDocs Home > BEA AquaLogic Data Services Platform Documentation > Data Services Developer's Guide > Contents

How To Create Conditional Elements in Return Types

This topic describes how to add a condition to a return type and determine the elements that are returned when the condition is true or false.

Add the Condition

A condition in a return type defines two groups of elements: those returned when an expression is true, and those returned when an expression is false. When you add a condition to a return type, you see two groups of return type elements.

To add a condition to the return type:

  1. Click the Query Map tab.
  2. Right-click an element in the return type, and choose Make Conditional.
    The conditional element is now duplicated.
     

Create the Expression

You must add the conditional expression, that determines which element is returned, in the XQuery source. You cannot add a conditional expression in the expression editor. 

  1. Click the Source tab.
    The primary Read function now has an if..else clause:
    declare function tns:read() as element(cus:CustomerOrder)*{
    for $CUSTOMER in cus1:CUSTOMER()
    return
        if (true()) then
           <cus:CustomerOrder>
    	 ...
           </cus:CustomerOrder>
        else
           <cus:CustomerOrder>
             ...
           </cus:CustomerOrder>
    };

    The expression after the if statement is evaluated, and the service returns either the first or second set of elements. The XQuery true() function simply returns the Boolean value true.

  2. In the XQuery source, replace true() with another XQuery expression, for example:
    if ( fn:data( $CUSTOMER/LAST_NAME ) = "Black" ) then

    You can use any XQuery expression that returns a value of true or false. In this example, if a customer has the last name Black, the first element group is returned. If not, the second element group is returned.
     
    To add the value of an element in a For block, use the XQuery fn:data function, which takes the value of an element:

    <LAST_NAME>Black</LAST_NAME>

  3. Click the Query Map tab.
  4. In the return type, add or delete elements in either group to create the return groups you want.
    Remember that the first group is returned if the expression is true, and the second group if the expression is false.
     

  5. Click the Test tab. Choose the Read function, and click Run. Check that the results are what you intend.
    In this example, the full group of elements is only returned for customers with the last name Black. For other customers, only the CUSTOMER_ID is returned.
     

See Also

How Tos
Other Resources
Document generated by Confluence on Apr 28, 2008 15:54